﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BigLotterySyndicate.Classes;
using SyndicateDAO.DatabaseObject;

namespace BigLotterySyndicate.Secured.Draws.Controls
{
    public partial class Base : BaseUserControl
    {
        public bool DrawLocked
        {
            get { return !DrawOpen; }
        }

        public bool DrawOpen
        {
            get { return this.CurrentDraw.status == draw.drawstatus.OPEN; }
        }

        public bool DrawPending
        {
            get { return this.CurrentDraw.status == draw.drawstatus.PENDING; }
        }

        public bool DrawFinished
        {
            get { return this.CurrentDraw.status == draw.drawstatus.FINISHED; }
        }

        public int ResultBonusBall
        {
            get
            {
                int result = 0;

                if (DrawFinished)
                {
                    string resultsString = this.BasePage.BusinessFacade.ResultBL.GetResultString(CurrentDraw.id);

                    var a = resultsString.Split(',').ToList();

                    var bonus = a.Skip(6).FirstOrDefault();
                    if(!String.IsNullOrEmpty(bonus))
                    return Int32.Parse(bonus);
                    else
                    {
                        return 0;
                    }

                }

                return result;
            }
        }

        public List<int> Result
        {
            get
            {
                List<int> result = new List<int>();

                if (DrawFinished)
                {
                    string resultsString = this.BasePage.BusinessFacade.ResultBL.GetResultString(CurrentDraw.id);

                    var a = resultsString.Split(',').ToList();

                    foreach (string item in a.Take(6))
                    {
                        result.Add(Int32.Parse(item));
                    }
                }

                return result;
            }

        }

        public List<int> ResultHealth
        {
            get
            {
                List<int> result = new List<int>();

                if (DrawFinished)
                {
                    string resultsString = this.BasePage.BusinessFacade.ResultBL.GetResultString(CurrentDraw.id);

                    var a = resultsString.Split(',').ToList();

                    foreach (string item in a.Take(5))
                    {
                        result.Add(Int32.Parse(item));
                    }
                }

                return result;
            }

        }


        public List<int> ResultEuro
        {
            get
            {
                List<int> result = new List<int>();

                if (DrawFinished)
                {
                    string resultsString = this.BasePage.BusinessFacade.ResultBL.GetResultString(CurrentDraw.id);

                    var a = resultsString.Split(',').ToList();

                    foreach (string item in a.Take(5))
                    {
                        result.Add(Int32.Parse(item));
                    }
                }

                return result;
            }

        }

        public List<int> ResultEuroLuckyStars
        {
            get
            {
                List<int> result = new List<int>();

                if (DrawFinished)
                {
                    string resultsString = this.BasePage.BusinessFacade.ResultBL.GetResultString(CurrentDraw.id);

                    var a = resultsString.Split(',').ToList();

                    foreach (string item in a.Skip(5))
                    {
                        result.Add(Int32.Parse(item));
                    }
                }

                return result;
            }

        }

        public List<string> ResultEuroRaffle
        {
            get
            {
                List<string> result = new List<string>();

                if (DrawFinished)
                {
                    string resultsString = this.BasePage.BusinessFacade.ResultBL.GetResultStringRaffle(CurrentDraw.id);

                    var a = resultsString.Split(',').ToList();

                    foreach (string item in a)
                    {
                        result.Add(item);
                    }
                }

                return result;
            }

        }

        public int? DrawId
        {
            get { return Request["dId"] != null ? Int32.Parse(Request["dId"].ToString()) : default(int?); }
        }

        public IQueryable<line> Lines
        {
            get { return this.BasePage.BusinessFacade.DrawBL.GetLines(CurrentDraw.id); }
        }

        public IQueryable<player> Players
        {
            get { return this.BasePage.BusinessFacade.DrawBL.GetPlayers(CurrentDraw.id); }
        }

        public IQueryable<player> PaidPlayers
        {
            get { return this.BasePage.BusinessFacade.DrawBL.GetPaidPlayers(CurrentDraw.id); }
        }

        public IQueryable<player> FreePlayers
        {
            get { return this.BasePage.BusinessFacade.DrawBL.GetFreePlayers(CurrentDraw.id); }
        }

        public draw CurrentDraw
        {
            get
            {
                if (DrawId.HasValue)
                    return this.BasePage.BusinessFacade.DrawBL.GetSingleById(this.DrawId.Value);
                else
                    return default(draw);
            }
        }

        public int RemainingFunds
        {
            get {

                int costs = 0;

               
                costs = Lines.Count() * LinePrice;
                
                int income = (PaidPlayers.Count() * LinePrice) + (int)CurrentDraw.extraFunds;

                return income - costs;
            
            
            }


        }

        public int LinePrice
        {
            get {

                
                    if ((int)CurrentDraw.pricePerLine != 0)
                        return (int) CurrentDraw.pricePerLine;
                    
                    if (CurrentDraw.type != draw.drawType.EUROMILLIONS)
                        return 1;
                    else
                        return 2;
            }
        }

        public string PrintDrawDate()
        {
            return this.CurrentDraw.drawdate.ToString("dd MMMM yyyy");
        }

        public string PrintDrawId()
        {
            return this.DrawId.HasValue ? this.DrawId.Value.ToString() : "0";
        }

        public string PrintDrawType()
        {
            return this.CurrentDraw.type;
        
        }

        public string PrintDrawPricePerLine()
        {
            if ((int) this.CurrentDraw.pricePerLine == 0)
            {

                if (this.CurrentDraw.type != draw.drawType.EUROMILLIONS)
                {
                    return "£1";
                }
                else
                {
                    return "£2";
                }

            }
            return String.Format("£{0}", this.CurrentDraw.pricePerLine.ToString("0.00"));
        }


        public string PrintResult()
        {
            return this.BasePage.BusinessFacade.ResultBL.GetResultString(CurrentDraw.id);
        }

        public string PrintLinesCount()
        {
          return  Lines.Count().ToString();
        }
        public string PrintPlayersCount()
        {
            return Players.Count().ToString();
        }

        public string PrintDrawCosts()
        {            
                return String.Format("&pound;{0:0.00}", Lines.Count() * LinePrice);
        }

        public string PrintFundsFromPlayers()
        {
            
                return String.Format("&pound;{0:0.00}", PaidPlayers.Count() * LinePrice);
        }

        public string PrintTotalFunds()
        {
            return String.Format("&pound;{0:0.00}", this.CurrentDraw.extraFunds + (PaidPlayers.Count() * LinePrice));
        }

        


        public string PrintFundsFromWinnings()
        {
            return String.Format("&pound;{0:0.00}", CurrentDraw.extraFunds);
        }

        public string PrintRemaining()
        {
            return String.Format("&pound;{0:0.00}", this.RemainingFunds);
        }
        public string PrintRemainingWithMinus()
        {
            string inMinus = "";
            if (this.RemainingFunds < 0)
                inMinus = "style=\"color:red;\"";

            return String.Format("<span {1}>&pound;{0:0.00}</span>", this.RemainingFunds,inMinus);
        }
    }
}